home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
EDUCATE
/
PCLEARN.ARJ
/
PCL.LZH
/
BATCH.TUT
< prev
next >
Wrap
Text File
|
1991-05-06
|
34KB
|
733 lines
████████████████████████████████████████████████████████████████
BATCH FILES FOR EFFICIENCY - GETTING THE WORK DONE
████████████████████████████████████████████████████████████████
A batch file contains within it a series of DOS commands in a
list. The commands may be either DOS other special batch
commands you wish the computer to perform in sequence. These
operations can be DOS operations (e.g., format a disk, display
the date and time, etc) or other operations which load or start
another program. Batch files are VERY useful!
Another way to think about a batch file is that it takes the
place of your keyboard and issues commands one after another
until it reaches a conclusion. Batch files operate line by line
and are read directly from the disk which makes them a little
slow, but nevertheless useful and extremely flexible.
Batch files can even prepare a menu system for your hard drive
and spare you the expense of buying a menu program from commercial
sources. Working with batch files means you are programming in
the most literal sense!
Each line of a batch file contains one instruction or operation
which the computer is to perform. Below is a listing for a very
simple batch file example. Don't worry about understanding it
just yet, simply note that each instruction is a DOS command on
a separate line . . .
EXAMPLE BATCH FILE EXPLANATION
| |
date date displayed
time time displayed
ver DOS version displayed
dir a:/p directory of a: floppy
displayed with a pause
The primary use of batch files is to automate sequences or
instructions which you use frequently. A batch file always has
the extension BAT. Example: hello.bat
Each line in the batch file is a separate command and is
performed AS IF you had typed in the command from your keyboard
at the DOS prompt! In addition to the usual DOS commands, batch
files can also contain special batch commands which allow you to
build small programs. We will talk about the special batch
commands a little later in this tutorial. Branching (selecting
various directions or choices) and iteration (repetitions of a
command) are allowed within the framework of these unique batch
commands. In addition, batch files may have special parameters
or inputs passed to them at the time you run the batch file from
the DOS command line or prompt. Batch files are POWERFUL and can
control many operations! They are also fun and easy to try.
A batch file is run or started by typing the file name without
the extension. This of course applies to all files ending with
file extensions BAT, EXE or COM.
Example: a>hello Then pressing enter or return key starts the
batch file hello.bat
Example: c>whoops starts the file whoops.exe
To terminate or stop any batch file in progress issue the break
command which uses the two key combination CONTROL-BREAK (hold
down the control CTRL key then press the break key) or
CONTROL-SCROLL LOCK.
Let's prepare a batch file:
First make sure you have a formatted disk in your disk drive and
DOS is displaying a DOS prompt such as a>. We will be using the
command COPY CON (copy data from the CONsole) command. Note that
any ASCII (plain english) text word processor or even EDLIN on
your DOS disk can be used to prepare a batch file; COPY CON is
only one of several alternatives. You can use either upper or
lower case to prepare batch files (capitals or small letters.)
Using COPY CON is like using a small typewriter to prepare your
batch file.
Type the following list when you have a DOS prompt such as a>
and a blank formatted floppy disk in the drive to accept the batch
file.
copy con 1.bat (press enter) (cursor skips to new line)
echo Hello there (press enter)
ver (press enter)
date (press enter)
dir/p (press enter)
^Z (press F6 OR hold control AND Z,
then press enter)
When done you'll have prepared a batch file of DOS commands
named 1.bat. This small file will print "hello there" then type
the DOS version in use then display date and finally produce a
directory listing and pause after each screenful. At this point
the batch file ends and returns you to DOS. In the first line we
use COPY CON to begin to prepare a batch file named 1.bat. In
the last line the ^Z means end of batch file preparation - exit
back to DOS. To run or execute the batch file since its name is
1.bat, just type 1 (then press enter key) at the DOS prompt.
Another example batch file for you to try:
copy con f.bat (press enter)
cls (press enter)
pause (press enter)
format b: (press enter)
echo all done (press enter)
^Z (press F6) (press enter)
This batch file (activated by typing f then enter) will clear
the screen then prepare to format a blank disk in b: drive - you
MUST have FORMAT.COM on the same disk as this batch file -
remember that format is an EXTERNAL command. After the batch
file has formatted the disk it prints "all done" on the screen.
Ctrl-Break key combination will halt any batch file operation
if you wish!
The next batch file might be used to backup wordprocessing data
files which end in the file extension TXT (such as ACCOUNTS.TXT)
from a source disk in the a: floppy drive to the b: floppy
drive. You could name it B.BAT and when you need to backup
simply put the master data disk in the a: drive and the backup
disk in the b: drive and type B at the DOS prompt. We've omitted
the copy con command at the top of the file and the ^Z at the
end since you already know how to start and end a batch file.
REM This batch file backs up files ending in TXT to backup disk B:
ECHO READY TO BACKUP. SOURCE DISK IN A: DRIVE, TARGET IN B: ?
PAUSE
COPY A:*.TXT B:
ECHO All done!
One batch file can start or call another, but the original batch
file cannot usually be returned to - you must continue on within
the second batch file.
If a batch file contains a typing or syntax error in any of its
commands, the computer will stop execution at that point and
return you to DOS. DOS remembers which disk contains the batch
file and the drive it was in. If you remove the original disk,
DOS will ask you to replace it so it can finish executing the
batch file. Batch files execute one step at a time from the disk
and NOT from RAM memory. This disk-based nature of batch files
make them a little slow, but they get the work done eventually.
Special batch file COMPILER utilities exist which speed
execution of batch files. Contact the author of PC-LEARN if you
are curious and want to make your batch files fly! This is
highly recommended if you want to add SPEED to batch files.
A superlative book on batch files you might wish to investigate
is MOS-DOS Batch File programming by Ronny Richardson, 1988,
Wincrest Books.
You should also investigate the superb SEBFU (Scanlon
Enterprises Batch File Utilities) shareware software package
which is a series of small batch file utilities which offer
an improvement over the standalone DOS batch file programming
language. SEBFU allows the user to produce subtle, powerful batch
files and includes an excellent tutorial about using batch files
for productivity. If you wish to try SEBFU write for the shareware
version, and include $5 for shipping and handling. Of course,
register the package if you find it useful after evaluation, as
with all shareware packages. For the SEBFU shareware disk:
Scanlon Enterprises
38354 17th ST E #C
Palmdale, CA 93550
(805) 272-4827
Remember the VDISK command in our second DOS tutorial? Many DOS
experts put commonly used batch files in the virtual or RAM disk
where a batch file really flies! This is one trick which can
turbocharge your batch file operations. Instead of executing in
slow fashion from a disk drive, the batch file is forced to run
from a speedy ramdisk!
████████████████████████████████████████████████████████████████
AUTOEXEC.BAT FILE BASICS - THE WAKEUP CALL TO YOUR COMPUTER
████████████████████████████████████████████████████████████████
The AUTOEXEC.BAT file starts your computer exactly the way you
want. It allows you to customize the machine to your liking as
the computer comes to life. You can cause the AUTOEXEC.BAT file
to print a startup menu of choices, load one particular program,
execute another batch file or other useful tasks. The
AUTOEXEC.BAT file is the first file DOS runs after loading
itself and configuring the computer. The AUTOEXEC.BAT file must
be on the same disk as DOS when the computer starts.
The AUTOEXEC.BAT file is a special batch file which MUST be
placed in the main or root directory of a disk to function
properly.
An AUTOEXEC.BAT file can always be modified, enlarged, edited,
or deleted later as you wish. Sometimes it is useful to have
several AUTOEXEC.BAT files. Each on a different startup disk to
operate different programs! An AUTOEXEC.BAT file (like all BAT
or batch files) can be modified with any common word processor.
You don't have to use the copy con facility each time! DOS EDLIN
line editing word processor will also do. EDLIN is covered in
your DOS manual.
Make sure you have a formatted disk in your disk drive and DOS
is displaying the usual DOS prompt when you construct a batch
file. Make sure you do not accidentally over-write or destroy
your current AUTOEXEC.BAT file! If necessary, rename your
current AUTOEXEC.BAT file (using the rename or REN command)
and make a new file while saving the old one "just in case."
Never edit on your original DOS disk, work on a copy!
Examine the next batch file preparation sequence:
copy con AUTOEXEC.BAT (press enter)
123 (press enter)
^Z (press enter)
This means (first line) create a file named AUTOEXEC.BAT as
typed from the keyboard or con (console). The (second line)
start program named 123. Final line end of batch file
preparation - stash it on the disk.
When finished you'll see a file named AUTOEXEC.BAT on your
directory listing screen which contains automatic startup
instructions. If this file were placed on your main DOS disk it
would try to start a program such as 123.EXE if such a program
existed there.
You can also start the AUTOEXEC.BAT by typing autoexec and then
pressing enter. To take a "peek" at the contents of an
AUTOEXEC.BAT file (or any bat file) simply use the type command.
Example: c>type AUTOEXEC.BAT (shows contents on your screen)
Example: a>type b:AUTOEXEC.BAT
Here is another AUTOEXEC.BAT file, this time from a hard drive
startup sequence; it provides a higher degree of control and
direction that a computer user might need for hard drive
customization.
copy con AUTOEXEC.BAT (press enter)
path \dos;\reflex;\wp;\util;\doc;\nor;\bat (press enter)
prompt $P$G (press enter)
cpu n (press enter)
verify on (press enter)
blank (press enter)
mode bw80,r (press enter)
dispclk (press enter)
type menu (press enter)
^Z (press F6 OR hold control AND Z, then press enter)
Let's examine this more complicated AUTOEXEC.BAT file in greater
detail:
The first line after "copy con AUTOEXEC.BAT" establishes a path
command to help DOS search every subdirectory on the hard disk
(you don't have to switch around to different areas of the disk,
DOS will search around for you since it now knows the various
"paths" to take.)
The second line alters the cursor prompt to tell you your
location and subdirectory.
The third line is a reference to the speed the computer will
operate at and is a unique command to a particular brand of
machine (cpu n means start the central processing unit chip at
normal speed.) Cpu is really CPU.COM, an external file which
sets the computer's processing speed.
The fourth line turns on the verify function for file copying.
The next line instructs the DOS mode function to switch to black
and white display, 80 columns wide and shift one column to the
right for alignment.
Next we ask DOS to tell us the time and date.
A final line instructs DOS to type to the screen a file
containing an ASCII test file prepared with an ordinary word
processor the menu for the monitor to display. Menu probably
gives us choices of programs to one and thus calls other
batch files such as 1.bat, 2.bat and so on.
The ^Z of course ends the file and instructs DOS to store the
file away for future use on the disk.
████████████████████████████████████████████████████████████████
BATCH FILE COMMANDS AND USE
████████████████████████████████████████████████████████████████
In addition to the normal DOS commands, batch files have their
own special subcommands. These commands are:
███ REM ███ The rem command sends a message to the screen or
simply to document or note a part of a batch file's operation.
You should use REM extensively to document long detailed batch
files so you can revise things and locate portions of the
program if you decide to change the batch file later. Remarks
can be up to 123 characters long. REM does not cause any
operation, it merely documents what you want to say or do.
Example: c>REM this is the location of menu operations
In DOS 2.0 the REM command could be replaced with a period or
dot, but this is not true in DOS 3.0 and above.
Example: c>. this is the location of menu operations
███ PAUSE ███ Stops batch file execution on a temporary basis
until you press a key. Thus you can pause a batch file and do
some operation (perhaps changing a floppy disk) and then
continue when you strike a key.
Example: b>PAUSE (no optional message included here)
Example: b>PAUSE This is an optional message, pardner!
███ ECHO ███ Turns display listing of commands on/off. It can
also send a message to the screen. It is frequently turned off
to remove excessive screen messages. Normally, with ECHO on,
screen messages are sent to the screen which can be distracting.
To suppress them use the first example. To restart the messages
use the second example. To add a message with the ECHO command
see example three. REM or remark command can also send a message
to the screen but NOT with ECHO turned off!
Example: a>ECHO OFF
Example: a>ECHO ON
Example: a>ECHO It's raining cats, dogs and computers
███ PARAMETERS AND MARKERS ████ This is NOT a batch file
command like ECHO or PAUSE.
Instead parameters are additional pieces of information or
"modifiers" which follow DOS commands.
Example: c>format b:/s
In the above, format is the command while b: and /s are the
parameters. Parameters modify the basic operation of a DOS
command but are not required by the command to operate. A batch
file can also accept parameters such as a word, filename,
symbol, drive letter or any useful character or group of
characters!
Markers placed inside the batch file listing signify which
parameter goes where. Markers are made from a percent sign (%)
and a single digit between 0 and 9 for a total of ten markers
available (remember, zero is a number too.) Here are the ten
markers:
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9
Let's use an example. Pretend that DOLITTLE.BAT is on your
floppy. Within its listing of commands there might be this
single line:
ECHO %0 %1 %2 (ECHO shows messages on the monitor)
If at the DOS prompt you typed:
b>DOLITTLE fancy pants (then press enter)
Your screen would show the following: b>ECHO DOLITTLE fancy
pants.
In this case, %0 has taken on the value at the start of the DOS
command which is the first word "DOLITTLE". Meanwhile %1 has
become "fancy" and %2 is now pants.
Looking at this another way:
DOLITTLE fancy pants
| | |
ECHO %0 %1 %2
Let's try an example. Pretend you had a large file of word
processing accounts for bills you have to pay from time to
time.
You need to look up bills or amounts in the file accounts.txt
which is in plain ASCII (english) text from your word processor.
The DOS FIND utility can search large files for specific words,
strings or characters. The general format for the FIND command
is: FIND "text" filename. FIND is located in the file FIND.COM
on your DOS disk and must be present with the batch file to be
used.
A simple batch file possibly named GET.BAT could do this:
ECHO searching for data . . . .
FIND "%1" %2
ECHO Finished, boss
Start the batch file get.bat with search data like this:
a>get grocery accounts.txt (first word starts get.bat, second
word is the item to search for, third item is the file to
search.)
As a result, you will get a report of the line where the word
"grocery" is found and any other data on the same line. This
could also be used to search a telephone list or list of employee
names and addresses. Pretty powerful idea for such a short
batch file!
███ GOTO ███ Jumps to a labeled set of commands within the
batch file. The general format for the command is GOTO LABEL
where LABEL is a line in the batch file which must start with a
colon (:) followed by a name up to eight characters long.
A simple, but useless batch file illustrates the GOTO command by
looping around in circles doing the same task endlessly.
Example listing for batch file:
:kitty
REM watch this fill your screen over and over, folks
GOTO kitty
This batch file will continue to print the remark line over and
over since it always returns to the start. Tap Ctrl-Break to
stop this silliness. The true usefulness of the GOTO command is
best understood by allowing the GOTO within a batch file to
transfer control elsewhere within its listing rather than to the
line immediately next in sequence. You can thus cause one thing
to happen (depending on a condition) or a different thing to
happen: choices and different outcomes!
███ IF ███ Allows conditional operation of a command. This is a
fancy way of saying you can cause a batch file to make decisions
based on a logical condition or input then do something. The
usual syntax of the IF command is IF CONDITION COMMAND. Let's
take this apart and examine the concept.
In the situation IF CONDITION COMMAND:
COMMAND is any normal DOS or batch file command and CONDITION is
one of three possible tests that yield true or false.
Example: IF %1==w GOTO dog (we'll explain this in a bit)
Example: IF %3 == 80 MODE BW80 (we'll explain this in a bit)
The three possible tests are:
1. The ERRORLEVEL condition (i.e., a specific number is found).
2. The STRING COMPARISON. (i.e., two strings are equivalent or
not.)
3. The FILE EXISTENCE condition. (i.e., if a file exists or not.)
In true full-featured programming languages many other logical
tests might be allowed, but for batch files these are these the
only three tests. Let's examine the three more closely. Then
illustrate with an example.
1. ERRORLEVEL is a number which tells DOS whether the last
program run was successful. If so the errorlevel is zero (0)
anything else above zero means unsuccessful.
2. STRING COMPARISON, the second conditional test, is always
indicated in a batch file by double equals signs (==). A test is
symbolically shown by the condition IF string1 == string2.
Frequently used with parameters or markers such as: IF %3 == 80
MODE BW80.
3. In the final and third conditional test, FILE EXISTENCE, the
usual format is IF EXIST d:filename.ext. which checks for a
certain file on a certain drive. You can thus check for a
certain disk or file before continuing the batch file process.
Pathnames are not allowed (d:\slip\and\slide).
Let's try a batch file example to illustrate the use of STRING
COMPARISONS to make a choice in how the batch file does its
work. Pretend you have two programs. One is a word processor
whose command to start is WORD and the other is a spreadsheet
whose command is LOTUS to start.
If we prepared a simple batch file called 1.bat whose listing is
below, we could start one or the other program by using either
the command:
a>1 w (to start the word processor OR
a>1 s (to start the spreadsheet).
The remarks (REM) in the example below give you a clue to the
construction of the batch file program but are not themselves
commands. The end result of this batch file is a saving of
keystrokes for two programs we might start often. You'll notice
we have omitted the copy con at the beginning and ^Z at the end
since you already know from previous examples how to start and
stop batch file preparation using those steps. The REM (remarks)
in this batch file are optional and could be omitted from the
real thing
REM This batch file selects one of two choices based on input
REM The next line turns off the screen echo to avoid screen clutter
ECHO OFF
REM Begin test for one of two choices
REM %1 in the next lines are markers for "w" or "s" keys.
IF %1==w GOTO dog
IF %1==s GOTO cat
REM Next line forces goto end if no match is made for w or s
GOTO end
:dog
REM Next command starts word processor
WORD
GOTO end
:cat
REM Next command starts spreadsheet
LOTUS
GOTO end
:end
REM Next line switches to root directory/leave the batch file
CD\
ECHO Batch file done, bye bye!
███ SHIFT ███ Re-assigns the relationship of parameters to
markers. It changes their values. And it does it in a very odd
way . . .
Remember that there are only ten markers available to a batch
file to hold the parameter values as we mentioned above. Here
they are:
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9
However you can raise the limit of 10 parameters in a batch file
using the single word SHIFT. When this command is encountered in
a batch file, all the parameter and marker pairings are shifted
one unit to the left. Whatever was assigned to %0 is lost.
A diagram to visualize. Before a SHIFT command is issued the
parameters and markers might be:
%0 %1 %2
| | |
dog cat computer
After the SHIFT command we would see:
%0 %1 %2
| | |
cat computer
Notice that dog is lost, %1 becomes computer and %2 is left
vacant unless it takes a new parameter from %3 (if %3 had a
parameter). The effects of the SHIFT command are wide ranging
throughout the batch file but provide great flexibility and a
range of parameters greater than ten values.
███ FOR..IN..DO ███ Allows iteration (repetition) of actions
or commands. The command is similar to a FOR...NEXT...STEP loop
programmers use.
The command is rather subtle and could be thought of as a three
part single line command. The syntax is on the next line:
FOR %%Variable IN (Set) DO Command
Let's look more closely at the three parts:
FOR %%Variable IN (Set) DO Command
============== ======= ==========
| | |
part 1 part 2 part 3
Translating into english it means something like: FOR a certain
batch file variable withIN a SET of filenames or commands DO a
certain action which DOS can carry out.
The %%VARIABLE is a one-letter variable which must have a double
%% prior to the letter to distinguish it from single % markers
we have seen earlier.
The SET portion of the command is always in parenthesis as
(SET). The SET represents filenames or DOS commands you wish the
%% variable to assume while the command is executing. A space is
used between entries. Pathnames are never allowed but wildcards
such as *.* are acceptable. If the SET contains DOS command then
only the %%VARIABLE is used.
The COMMAND is a DOS command or batch subcommand. One or several
of these commands will contain the %%Variable in it.
Let's try an example. Pretend by you want a batch file to see
the DOS version then clear the screen and finally issue the
directory. We could do this with a three line batch file but
FOR..IN..DO can do this in one:
Example: FOR %%T IN (Ver cls Dir/P) DO %%T
Notice in the above example how each DOS command is separated by
a space. ? and * are not allowed within any command within the
SET. Use a colon (:) instead of a space within the set when
passing parameters to programs. You can issue the FOR..IN..DO
batch file subcommand at the DOS prompt by dropping one of the
percentage signs ( % ) on the variable.
████████████████████████████████████████████████████████████████
A BATCH FILE SUMMARY AND POP QUIZ!
████████████████████████████████████████████████████████████████
Use your imagination to construct batch files to shorten your
DOS tasks.
Here is the source code text of GO.BAT which was used to control
an older version of PC-LEARN with a batch file menu!
You could use a batch file such as this to operate a menu for
your hard drive! By the way, for a lengthy batch file such as
this, you would probably use your word processor to prepare the
batch file rather than the tedious COPY CON command method used
earlier - just make sure that the final file is ASCII text; see
your word processor manual for notes on this technique. The basic
flow of this long batch file is to display a menu screen, wait for
a keypress by the user and then use a label system to branch to
the area of the program that displays a tutorial to the screen.
Menu batch file for an older version of PC-LEARN:
BATCH FILE COMMAND EXPLANATION
| |
ECHO OFF Cause screen NOT to display each batch file command
CLS Clear screen from any previous program display
DRIVER1 Run driver1.com to display cover screen of PC-LEARN
PAUSE Wait for keypress
:TOP Label marker, serves as a "bookmark"
DRIVER2 Run driver2.com, diplay main menu
:START Label marker, serves as a "bookmark"
DRIVER3 Run driver3.com, detect keypress for menu selection
ECHO ON Turn on echo to display following comment . . .
REM ----- WAIT FOR A MOMENT - DISK DRIVE LOADING DATA -----
ECHO OFF Turn echo back off to quench screen clutter
IF ERRORLEVEL 27 GOTO END "errorlevel" line tests keypress
IF ERRORLEVEL 21 GOTO START another errorlevel test
IF ERRORLEVEL 20 GOTO LABELT another error level test
IF ERRORLEVEL 19 GOTO LABELS
IF ERRORLEVEL 18 GOTO LABELR
IF ERRORLEVEL 17 GOTO LABELQ
IF ERRORLEVEL 16 GOTO LABELP
IF ERRORLEVEL 15 GOTO LABELO
IF ERRORLEVEL 14 GOTO LABELN
IF ERRORLEVEL 13 GOTO LABELM
IF ERRORLEVEL 12 GOTO LABELL
IF ERRORLEVEL 11 GOTO LABELK
IF ERRORLEVEL 10 GOTO LABELJ
IF ERRORLEVEL 9 GOTO LABELI
IF ERRORLEVEL 8 GOTO LABELH
IF ERRORLEVEL 7 GOTO LABELG
IF ERRORLEVEL 6 GOTO LABELF
IF ERRORLEVEL 5 GOTO LABELE
IF ERRORLEVEL 4 GOTO LABELD
IF ERRORLEVEL 3 GOTO LABELC
IF ERRORLEVEL 2 GOTO LABELB
:LABELA Destination label if keypress is "A"
view intro.tut Run view.com to display intro tutorial
GOTO TOP Goto label "top" when done
:LABELB Remaining lines similar to above
view history.tut
GOTO TOP
:LABELC
view dos1.tut
GOTO TOP
:LABELD
view dos2.tut
GOTO TOP
:LABELE
view batch.tut
GOTO TOP
:LABELF
view wordp.tut
GOTO TOP
:LABELG
view spread.tut
GOTO TOP
:LABELH
view databs.tut
GOTO TOP
:LABELI
view hd.tut
GOTO TOP
:LABELJ
view displstd.tut
GOTO TOP
:LABELK
view equipmnt.tut
GOTO TOP
:LABELL
view bibliog.tut
GOTO TOP
:LABELM
view usergrp.tut
GOTO TOP
:LABELN
view glossry.tut
GOTO TOP
:LABELO
view software.tut
GOTO TOP
:LABELP
view modem.tut
GOTO TOP
:LABELQ
view tips.tut
GOTO TOP
:LABELR
view virus.tut
GOTO TOP
:LABELS
view bonus.tut
GOTO TOP
:LABELT
view author.tut
GOTO TOP
:END
CLS
ECHO ON
REM ----- THANKS FOR USING PC-LEARN! END OF TUTORIAL SYSTEM -----
REM ----- REGISTERING PC-LEARN WILL BRING YOU THE BONUS DISK! ----